package com.sywb.chuangyebao.contract;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.sywb.chuangyebao.Constants;
import com.sywb.chuangyebao.R;
import com.sywb.chuangyebao.bean.Answer;
import com.sywb.chuangyebao.bean.Page;
import com.sywb.chuangyebao.utils.GlideUtils;
import com.sywb.chuangyebao.utils.HttpCallback;
import com.sywb.chuangyebao.utils.HttpUtils;
import com.sywb.chuangyebao.utils.RegexUtils;
import com.sywb.chuangyebao.utils.Utils;
import com.sywb.chuangyebao.widget.RichTextView;

import org.bining.footstone.adapter.BaseRecyclerAdapter;
import org.bining.footstone.adapter.ViewHolderHelper;
import org.bining.footstone.adapter.listener.OnItemChildClickListener;
import org.bining.footstone.utils.TimeUtils;
import org.bining.footstone.utils.ToastUtils;
import org.bining.footstone.view.BaseActivity;

public interface AnswerCommentContract {

    interface AnswerCommentView extends BaseRefreshContract.View {

        void setCommentCount(int commentCount);
    }

    class Presenter<T extends AnswerCommentView> extends BaseRefreshContract.Presenter<T> implements View.OnClickListener {

        protected ReplyAdapter recyclerAdapter;
        protected int pageNo = 1;
        protected Answer answer;
        protected Answer oAnswer;

        @Override
        public void onStart() {
            super.onStart();
            recyclerAdapter = new ReplyAdapter(mContext);
            setRecyclerAdapter(recyclerAdapter);
        }

        @Override
        public void onStartRefresh() {
            pageNo = 1;
            initCommentData();
        }

        @Override
        public void onStartLoadMore() {
            pageNo++;
            initCommentData();
        }

        public void initCommentData() {
            if (oAnswer == null) {
                return;
            }
            HttpUtils.listComments(oAnswer.answer_id, pageNo, oAnswer.isReply ? Constants.TOPIC_TYPE_REPLY : Constants.TOPIC_TYPE_COMMENT, oAnswer.node, new HttpCallback<Page<Answer>>() {

                @Override
                public void onSuccess(Page<Answer> commentPage) {
                    if (pageNo == 1) {
                        onEndRefresh();
                        recyclerAdapter.clearDatas();
                    }
                    recyclerAdapter.notifyDataChangedAfterLoadMore(commentPage.list);
                }

                @Override
                public void onError(String strMsg) {
                    super.onError(strMsg);
                    if (pageNo == 1) {
                        onEndRefresh();
                    } else {
                        onEndLoadMore();
                    }
                    showMessage(strMsg);
                }

                @Override
                public void onFinish() {
                    super.onFinish();
                    onFinishAsync();
                    isNoData();
                }
            });
        }

        public void isNoData() {
            if (mView != null) {
                mView.setNoData(recyclerAdapter.getDataCount() == 0);
                mView.setCommentCount(recyclerAdapter.getDataCount());
            }
        }

        @Override
        public void onClick(View view) {
            if (!((BaseActivity) mActivity).isCanClick(view)) return;
            switch (view.getId()) {
                case R.id.ll_answer_me:
                    break;
            }
        }

        public void publish(final EditText input) {
            //判断是否登录
            if (!Utils.checkIsNext(mView.getMyFragmentManager())) {
                return;
            }

            if (answer == null) {
                return;
            }

            String string = input.getText().toString();
            if (TextUtils.isEmpty(string)) {
                ToastUtils.show(mActivity, R.string.input_comment_content);
                return;
            }

            HttpUtils.saveComment(answer.answer_id, string, answer.relation_id, answer.node, answer.question_id, new HttpCallback<String>() {

                @Override
                public void onStart() {
                    super.onStart();
                    onStartAsync();
                }

                @Override
                public void onSuccess(String s) {
                    ToastUtils.show(mActivity, "评论成功!");
                    input.setText("");
                    answer.answer_num += 1;
                    mView.setCommentCount(answer.answer_num);
                    initCommentData();
                }

                @Override
                public void onError(String strMsg) {
                    super.onError(strMsg);
                    showMessage(strMsg);
                }

                @Override
                public void onFinish() {
                    super.onFinish();
                    onFinishAsync();
                }
            });
        }

        /**
         * 点赞、取消点赞
         * 调此方法前，需更新answer
         *
         * @param topic_Type 点赞类型
         */
        protected void agree(final String topic_Type) {
            //判断是否登录
            if (!Utils.checkIsLogin()) {
                return;
            }
            HttpUtils.agree(topic_Type, answer.answer_id, !answer.isZan, new HttpCallback<Object>() {

                @Override
                public void onStart() {
                    super.onStart();
                    onStartAsync();
                }

                @Override
                public void onSuccess(Object o) {
                    doOnAgreeSuccess(topic_Type, answer);
                }

                @Override
                public void onError(String strMsg) {
                    super.onError(strMsg);
                    showMessage(strMsg);
                }

                @Override
                public void onFinish() {
                    super.onFinish();
                    onFinishAsync();
                }
            });
        }

        protected void doOnAgreeSuccess(String topic_Type, Answer clickAnswer) {
            clickAnswer.isZan = !clickAnswer.isZan;
            clickAnswer.agree_num = clickAnswer.isZan ? clickAnswer.agree_num + 1 : clickAnswer.agree_num - 1;
        }

        class ReplyAdapter extends BaseRecyclerAdapter<Answer> implements OnItemChildClickListener {


            public ReplyAdapter(Context context) {
                super(context, R.layout.item_answer_reply);
            }

            @SuppressLint("SetTextI18n")
            @Override
            protected void setItemData(ViewHolderHelper viewHolderHelper, int i, Answer model) {
                viewHolderHelper.setOnItemChildClickListener(this);
                viewHolderHelper.setItemChildClickListener(R.id.iv_head);
                viewHolderHelper.setItemChildClickListener(R.id.iv_user_type);
                viewHolderHelper.setItemChildClickListener(R.id.tv_name);
                viewHolderHelper.setItemChildClickListener(R.id.iv_zan);
                viewHolderHelper.setItemChildClickListener(R.id.tv_zan_count);

                ImageView imageView = viewHolderHelper.getView(R.id.iv_head);
                GlideUtils.getCircleImageToUrl(mActivity, model.avatar, imageView);
                viewHolderHelper.setText(R.id.tv_name, model.display_name);
//                viewHolderHelper.setText(R.id.tv_name, "answerId:" + model.answer_id + "qId:" + model.question_id);
                viewHolderHelper.setText(R.id.tv_zan_count, RegexUtils.getZanNum(model.agree_num));
                RichTextView richTextView = viewHolderHelper.getView(R.id.tv_comment);
//                richTextView.setRichText(model.content + "    node:" + model.node,model.richInfoList);
                richTextView.setRichText(model.content, model.richInfoList);
                viewHolderHelper.setText(R.id.tv_time, TimeUtils.transformDate10(model.create_time));
                Utils.setUserRoleIcon((ImageView) viewHolderHelper.getView(R.id.iv_user_type), model.user_role);

                //回复个数
                TextView tvReply = viewHolderHelper.getView(R.id.tv_reply);
                if (model.isReply) {
                    //第一条为评论内容
                    tvReply.setVisibility(View.INVISIBLE);
                    viewHolderHelper.setVisibility(R.id.tv_point, View.INVISIBLE);
                } else {
                    tvReply.setVisibility(View.VISIBLE);
                    viewHolderHelper.setVisibility(R.id.tv_point, View.VISIBLE);

                    if (model.comment_num > 0) {
                        tvReply.setText(RegexUtils.getFormatNum(model.comment_num) + "回复");
                        tvReply.setBackgroundResource(R.drawable.shape_detail_comment);
                        int dp4 = mActivity.getResources().getDimensionPixelSize(R.dimen.dp_4);
                        int dp12 = mActivity.getResources().getDimensionPixelSize(R.dimen.dp_12);
                        tvReply.setPadding(dp12, dp4, dp12, dp4);
                    } else {
                        tvReply.setText("回复");
                        tvReply.setBackgroundColor(Color.TRANSPARENT);
                        tvReply.setPadding(0, 0, 0, 0);
                    }
                }

                //是否赞
                viewHolderHelper.setImageResource(R.id.iv_zan, model.isZan ? R.drawable.praise_red : R.drawable.praise);
                viewHolderHelper.setTextColorRes(R.id.tv_zan_count, model.isZan ? R.color.messageRed : R.color.colorGray);

            }

            @Override
            public void onItemChildClick(View view, int i) {
//                Logger.e("点击了哪个：" + JSON.toJSONString(getItem(i - 1)));
                initClickedItem(i);
                switch (view.getId()) {
                    case R.id.iv_head:
                    case R.id.iv_user_type:
                    case R.id.tv_name:
//                        mView.advance(UserIndexActivity.class, getItem(i - 1).user_id);
                        if (i == 0) {
                            return;
                        }
                        Utils.goUserIndex(mView, getItem(i - 1).user_id);
                        break;
                    case R.id.tv_zan_count:
                    case R.id.iv_zan:
                        agree(oAnswer.isReply && i > 0 ? Constants.TOPIC_TYPE_REPLY : Constants.TOPIC_TYPE_COMMENT);
                        break;
                    case R.id.tv_comment:
                        onRecyclerItemClick(i);
                        break;
                }
            }
        }

        protected void initClickedItem(int i) {

        }
    }

}

